<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
    JSONObject jsonData = new JSONObject();
    String ids = Util.null2String( request.getParameter( "ids" ) );
    String userid = "139";
    int successcount = 0;
    int failurecount = 0;
    rs.executeQuery( " select id,gdwybs,rwdwybs,gzxwybs,suqiuren,yqhfjzrq,gdbh,convert(rxbyj,char) as rxbyj," +
            "xqzhm,requestname,qzrq,convert(nr,char) as nr,pssj,email,shouji,dz,fj,wd  " +
            "from uf_dpfdb  where id in ("+ids+")");
    int counts = rs.getCounts();
    while (rs.next()){
        String id = Util.null2String( rs.getString("id"));
        String yqhfjzrq = Util.null2String( rs.getString("yqhfjzrq"));
        String subrq =yqhfjzrq.substring( 0,10 );
        String subsj =yqhfjzrq.substring( 11,16 );
        String requestname = Util.null2String( rs.getString("requestname"));
        String qzrq = Util.null2String( rs.getString("qzrq"));
        String pssj = Util.null2String( rs.getString("pssj"));
        Map<String, String> requestMainMap = new HashMap<>(  );
        requestMainMap.put( "suqiuren",Util.null2String( rs.getString("suqiuren")) );
        requestMainMap.put( "pssj",!"".equals(pssj)?pssj.substring( 0,10 ):"" );
        requestMainMap.put( "qzhm",Util.null2String( rs.getString("xqzhm")));
        requestMainMap.put( "qzrq",!"".equals(qzrq)?pssj.substring( 0,16 ):"" );
        requestMainMap.put( "rxbyj",Util.null2String( rs.getString("rxbyj")) );
        requestMainMap.put( "nr",Util.null2String( rs.getString("nr")) );
        requestMainMap.put( "email",Util.null2String( rs.getString("email")) );
        requestMainMap.put( "shouji",Util.null2String( rs.getString("shouji")) );
        requestMainMap.put( "dz",Util.null2String( rs.getString("dz")) );
        requestMainMap.put( "yqhfjzrq",subrq);
        requestMainMap.put( "yqhfjzsj",subsj);
        requestMainMap.put( "fjsc",Util.null2String( rs.getString("fj")));
        requestMainMap.put( "lzfj",Util.null2String( rs.getString("wd")));
        requestMainMap.put( "gdwybs",Util.null2String( rs.getString("gdwybs")));
        requestMainMap.put( "rwdwybs",Util.null2String( rs.getString("rwdwybs")));
        requestMainMap.put( "gzxwybs",Util.null2String( rs.getString("gzxwybs")));
        requestMainMap.put( "gdbh",Util.null2String( rs.getString("gdbh")));
        String requestid = WorkflowCreateByRequestMap(userid,"45",requestname,"0",requestMainMap);
        if(Util.getIntValue( requestid )>0){
            rs.executeUpdate("update uf_dpfdb set czzt=1 where id=?",id);
            successcount++;
        }else {
            failurecount++;
        }
    }

    jsonData.put( "counts", counts );
    jsonData.put( "successcount", successcount );
    jsonData.put( "failurecount", failurecount );
    out.print( jsonData.toJSONString() );
%>
<%!
    BaseBean baseBean = new BaseBean();

    //创建流程方法
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        baseBean.writeLog( "-----创建流程传递的参数个数--------" );
        baseBean.writeLog( "creatorId:" + creatorId );
        baseBean.writeLog( "workflowId:" + workflowId );
        baseBean.writeLog( "requestName:" + requestName );
        baseBean.writeLog( "IsNextFlow:" + IsNextFlow );

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid( creatorId );//创建人Id
        requestInfo.setWorkflowid( workflowId );//工作流Id
        requestInfo.setDescription( requestName );//请求标题
        if ("0".equals( IsNextFlow )) {
            requestInfo.setIsNextFlow( IsNextFlow );
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName( Util.null2String( entry.getKey() ) );
            propertyArray[p].setValue( Util.null2String( entry.getValue() ) );
            baseBean.writeLog( "\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue() );
            p++;
        }
        baseBean.writeLog( "-----创建流程传递的参数个数p=" + p );
        mainTableInfo.setProperty( propertyArray );
        requestInfo.setMainTableInfo( mainTableInfo );
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest( requestInfo );//创建请求id
            } catch (Exception e) {
                baseBean.writeLog( "=======" + e.getMessage() );
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer( "\r\n-----xwd创建工作流记录日志开始" );
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append( "\r\n-----姓名:" + rscif.getLastname( rcif.getRequestCreater( requestid ) ) );
            sbf.append( "\r\n-----时间:" + rcif.getRequestCreateTime( requestid ) );
            sbf.append( "\r\n-----创建流程:" + wfcif.getWorkflowname( workflowId ) );
            sbf.append( "\r\n-----请求:" + rcif.getRequestname( requestid ) );
            sbf.append( "\r\n-----请求:" + requestid );
            sbf.append( "\r\n-----创建工作流记录日志结束" );
            baseBean.writeLog( sbf.toString() );
        } catch (Exception e) {
            baseBean.writeLog( "错误：" + e );
        }
        return requestid;
    }

%>